package csu.rjxyg.factory.core;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import csu.rjxyg.util.DBUtil;
import csu.rjxyg.util.DataUtil;
import csu.rjxyg.util.FileUtil;
import csu.rjxyg.util.LogUtil;
import csu.rjxyg.util.SortUtil;
import csu.rjxyg.util.TimeUtil;

public class UtilFactory extends BaseFactory{
	private final static String UTIL_LOG = "LogUtilFactoryName";
	private final static String UTIL_FILE = "FileUtilFactoryName";
	private final static String UTIL_DB = "DBUtilFactoryName";
	private final static String UTIL_TIME	=	"TimeUtilFactoryName";
	private final static String UTIL_SORT	=	"SortUtilFactoryName";
	private final static String UTIL_DATA		=		"DataUtilFactoryName";
	private final static String FILE_NAME = BaseFactory.FILE_NAME;
	

	
	
	
	public static FileUtil getFileUtil(String fileName){
		try{
			DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
	        DocumentBuilder builder = dFactory.newDocumentBuilder();
	        Document doc;
	        File file = new File(FILE_NAME);
	        doc = builder.parse(file);
	          
	        //获取包含类名的文本节点
	        String cName;
	        NodeList nl = doc.getElementsByTagName(UTIL_FILE);
	        Node temp = nl.item(0).getFirstChild();
	        cName = temp.getNodeValue();
	        FileUtil fileUtil =  (FileUtil) Class.forName(cName).newInstance();
	        fileUtil.setFileName(fileName);
	        return fileUtil;
		        
		}catch(Exception e){
			e.printStackTrace();
		}
			System.out.println("装载文件工具类失败");
			return null;
	}
	
	public static DBUtil getDBUtil(){
		try{
			DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
	        DocumentBuilder builder = dFactory.newDocumentBuilder();
	        Document doc;
	        File file = new File(FILE_NAME);
	        doc = builder.parse(file);
	          
	        //获取包含类名的文本节点
	        String cName;
	        NodeList nl = doc.getElementsByTagName(UTIL_DB);
	        Node temp = nl.item(0).getFirstChild();
	        cName = temp.getNodeValue();
	        
	        return  (DBUtil) Class.forName(cName).newInstance();
		        
		}catch(Exception e){
			e.printStackTrace();
		}
			System.out.println("装载数据库工具类失败");
			return null;
	}
	
	public static LogUtil getLogUtil(Class c){
		try{
			DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
	        DocumentBuilder builder = dFactory.newDocumentBuilder();
	        Document doc;
	        File file = new File(FILE_NAME);
	        doc = builder.parse(file);
	          
	        //获取包含类名的文本节点
	        String cName;
	        NodeList nl = doc.getElementsByTagName(UTIL_LOG);
	        Node temp = nl.item(0).getFirstChild();
	        cName = temp.getNodeValue();
	        LogUtil logUtil= (LogUtil) Class.forName(cName).newInstance();
	        logUtil.setClass(c);
	        return logUtil ;
		        
		}catch(Exception e){
			e.printStackTrace();
		}
			System.out.println("装载日志工具类失败");
			return null;
	}
	
	public static TimeUtil getTimeUtil(){
		try{
			DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
	        DocumentBuilder builder = dFactory.newDocumentBuilder();
	        Document doc;
	        File file = new File(FILE_NAME);
	        doc = builder.parse(file);
	          
	        //获取包含类名的文本节点
	        String cName;
	        NodeList nl = doc.getElementsByTagName(UTIL_TIME);
	        Node temp = nl.item(0).getFirstChild();
	        cName = temp.getNodeValue();
	        TimeUtil timeUtil= (TimeUtil) Class.forName(cName).newInstance();
	        return timeUtil ;
		        
		}catch(Exception e){
			e.printStackTrace();
		}
			System.out.println("装载时间工具类失败");
			return null;
	}
	
	public static SortUtil getSortUtil(){
		try{
			DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
	        DocumentBuilder builder = dFactory.newDocumentBuilder();
	        Document doc;
	        File file = new File(FILE_NAME);
	        doc = builder.parse(file);
	          
	        //获取包含类名的文本节点
	        String cName;
	        NodeList nl = doc.getElementsByTagName(UTIL_SORT);
	        Node temp = nl.item(0).getFirstChild();
	        cName = temp.getNodeValue();
	        SortUtil sortUtil= (SortUtil) Class.forName(cName).newInstance();
	        return sortUtil ;
		        
		}catch(Exception e){
			e.printStackTrace();
		}
			System.out.println("装载时间工具类失败");
			return null;
	}
	
	public static DataUtil getDataUtil(){
		try{
			DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
	        DocumentBuilder builder = dFactory.newDocumentBuilder();
	        Document doc;
	        File file = new File(FILE_NAME);
	        doc = builder.parse(file);
	          
	        //获取包含类名的文本节点
	        String cName;
	        NodeList nl = doc.getElementsByTagName(UTIL_DATA);
	        Node temp = nl.item(0).getFirstChild();
	        cName = temp.getNodeValue();
	        DataUtil dataUtil= (DataUtil) Class.forName(cName).newInstance();
	        return dataUtil ;
		        
		}catch(Exception e){
			e.printStackTrace();
		}
			System.out.println("装载数据读取工具类失败");
			return null;
	}
}